package com.company.oa.hr.controller.employee;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.hr.dto.employee.EmployeeDTO;
import com.company.oa.hr.entity.employee.Employee;
import com.company.oa.hr.service.employee.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hr/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 分页查询员工列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('hr:employee:list')")
    public Result<IPage<Map<String, Object>>> getEmployeePage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Employee employee) {
        IPage<Map<String, Object>> pageResult = employeeService.getEmployeePage(page, size, employee);
        return Result.success(pageResult);
    }

    /**
     * 获取员工详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:employee:info')")
    public Result<Map<String, Object>> getEmployeeDetail(@PathVariable Long id) {
        Map<String, Object> employee = employeeService.getEmployeeDetail(id);
        return Result.success(employee);
    }

    /**
     * 创建员工
     */
    @PostMapping
    @PreAuthorize("hasAuthority('hr:employee:create')")
    public Result<Boolean> createEmployee(@RequestBody @Valid EmployeeDTO employeeDTO) {
        // 从当前登录用户中获取创建人ID
        Long creatorId = 1L; // 实际项目中应从SecurityContext中获取
        boolean success = employeeService.createEmployee(employeeDTO, creatorId);
        return Result.success(success);
    }

    /**
     * 更新员工信息
     */
    @PutMapping
    @PreAuthorize("hasAuthority('hr:employee:update')")
    public Result<Boolean> updateEmployee(@RequestBody @Valid EmployeeDTO employeeDTO) {
        boolean success = employeeService.updateEmployee(employeeDTO);
        return Result.success(success);
    }

    /**
     * 删除员工
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:employee:delete')")
    public Result<Boolean> deleteEmployee(@PathVariable Long id) {
        boolean success = employeeService.deleteEmployee(id);
        return Result.success(success);
    }

    /**
     * 员工离职
     */
    @PostMapping("/resign/{id}")
    @PreAuthorize("hasAuthority('hr:employee:resign')")
    public Result<Boolean> employeeResign(
            @PathVariable Long id,
            @RequestParam String reason,
            @RequestParam Date resignDate) {
        boolean success = employeeService.employeeResign(id, reason, resignDate);
        return Result.success(success);
    }

    /**
     * 批量导入员工
     */
    @PostMapping("/import")
    @PreAuthorize("hasAuthority('hr:employee:import')")
    public Result<Map<String, Object>> importEmployees(@RequestParam("file") MultipartFile file) {
        // 从当前登录用户中获取创建人ID
        Long creatorId = 1L; // 实际项目中应从SecurityContext中获取
        Map<String, Object> result = employeeService.importEmployees(file, creatorId);
        return Result.success(result);
    }

    /**
     * 导出员工信息
     */
    @PostMapping("/export")
    @PreAuthorize("hasAuthority('hr:employee:export')")
    public Result<byte[]> exportEmployees(@RequestBody List<Long> employeeIds) {
        byte[] excelData = employeeService.exportEmployees(employeeIds);
        return Result.success(excelData);
    }
} 